package com.wm.machine.baselibrary.common.widget.recyclerview.itemdecoration;

import android.graphics.Canvas;
import android.graphics.Rect;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

/**
 * Copyright
 * author zhengcheng
 * date 2018/2/12
 * describe recyclerview 间隔设置(代理模式)
 * history
 */

public class SpaceItemDecoration extends RecyclerView.ItemDecoration {
  private RecyclerView.ItemDecoration mEntrust;
  private int leftRight;
  private int topBottom;
  private int mColor =0;

  public SpaceItemDecoration(int leftRight, int topBottom) {
    this.leftRight = leftRight;
    this.topBottom = topBottom;
  }

  public SpaceItemDecoration(int leftRight, int topBottom, int mColor) {
    this(leftRight, topBottom);
    this.mColor = mColor;
  }

  @Override
  public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
    super.onDraw(c, parent, state);
    mEntrust = getEntrust(parent.getLayoutManager());
    if (mEntrust != null) {
      mEntrust.onDraw(c, parent, state);
    }
  }

  @Override
  public void getItemOffsets(Rect outRect, View view, RecyclerView parent,
      RecyclerView.State state) {
    super.getItemOffsets(outRect, view, parent, state);
    mEntrust = getEntrust(parent.getLayoutManager());
    if (mEntrust != null) {
      mEntrust.getItemOffsets(outRect, view, parent, state);
    }
  }

  /**
   * 
   * @param layoutManager 布局管理器类型
   * @return entrust
   */
  public RecyclerView.ItemDecoration getEntrust(RecyclerView.LayoutManager layoutManager) {
    if (layoutManager != null) {
      if (layoutManager instanceof GridLayoutManager) {
        mEntrust = new GridEntrust(leftRight, topBottom, mColor);
      } else if (layoutManager instanceof LinearLayoutManager) {
        mEntrust = new LinearEntrust(leftRight, topBottom, mColor);
      }
    }
    return mEntrust;
  }
}
